How is this recursion properly working when it is iterated through [on hold]

Posted by Rakso Zrobin on Programmers See other posts from Programmers or by Rakso Zrobin
Published on 2013-10-23T01:40:57Z Indexed on 2013/10/23 4:09 UTC
Read the original article Hit count: 263

Filed under:
|

Here is my code right now:

hasht= {"A":["B", "D", "E"], 
        "B":["C"], 
        "C":["D", "E"], 
        "D":["C", "E"], 
        "E":["B"]}
paths=[]
def recusive(start, finish, started=true):
    if start==finish and !started:
        return start
    else:
        for i in hasht[start]:
            path= start+ recusive(i,finish,false)
            paths.append(path)
print (recusive("C","C",1))
print paths

# [CDC, CDEBC, CEBC]

I am trying to generate a table like the one on the bottom, but am running into the problem of the string and the array not being able to concatenate. When I just return however, it returns CDC and works, however, exiting the function as return is meant to do.

I am wondering how I can improve my code here to (1) make it work, (2) why my logic was faulty. For example, I understand that it generates say [DC], but I am confused as to how to go around that. perhaps index the value returned?

© Programmers or respective owner

Related posts about python

Related posts about recursion